const app = getApp()
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerRoomList,
  reqConsumerRoomAdd,
} from '../../../../request/api.js';
Page({
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    familyInfo: {},

    dataList: [],
    showInputDialog: false,
  },

  onLoad(options) {
    this.getParams()

  },


  onShow() {

  },

  getParams() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendDataToRoom', function (res) {
      _this.setData({
        familyInfo: res.data
      })
      console.log('form', _this.data.familyInfo);
      _this.getDataList()
    })
  },

  async getDataList() {
    let params = {
      homeId: this.data.familyInfo.id,
      // homeId: '1906590101087543298',
    }
    const res = await reqConsumerRoomList(params)
    if (res.code !== 20000) return showMsg(res.message)
    let {
      rows
    } = res.data
    this.setData({
      dataList: rows || []
    })

  },

  goAdd() {
    this.setData({
      showInputDialog: true
    })
    // const _this = this
    // wx.navigateTo({
    //   url: '/packageC/pages/room/add/add',
    //   events: {
    //     receiveDataFromAdd: async function (res) {
    //       if (res.isUpdataList) {
    //         await _this.getDataList()
    //         _this.notifyPageUpdataFoo()
    //       }
    //     },
    //   },
    //   success: function (res) {
    //     res.eventChannel.emit('sendDataToAdd', {
    //       data: _this.data.familyInfo
    //     })
    //   }
    // })
  },

  async inputDialogHandle(e) {
    const {
      actionType,
      value
    } = e.detail
    this.setData({
      showInputDialog: false
    })
    if (actionType !== 'confirm') return
    const res = await reqConsumerRoomAdd({
      roomName:value,
      // homeId: app.globalData.familyInfo.id,
      homeId: this.data.familyInfo.id,
    })
    if (res.code != 20000) return showMsg(res.message);
    this.setData({
      showInputDialog: false
    })
    app.globalData.isUpdateCIndex = true
    wx.setStorageSync('isUpdateCIndex', true);
    this.getDataList()
  },

  goDetail(e) {
    const _this = this
    const {
      item
    } = e.currentTarget.dataset
    console.log(item);
    wx.navigateTo({
      url: '/packageC/pages/room/detail/detail',
      events: {
        receiveDataFromDetail: function (res) {
          if (res.isUpdataList) {
            _this.getDataList()
          }
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendDataToDetail', {
          data: item
        })
      }
    })
  },

  goEdit() {
    const _this = this
    const {
      dataList
    } = this.data
    wx.navigateTo({
      url: '/packageC/pages/room/edit/edit',
      events: {
        receiveDataFromEdit: async function (res) {
          if (res.isUpdataList) {
            await _this.getDataList()
            _this.notifyPageUpdataFoo()
          }
        },
      },
      success: function (res) {
        res.eventChannel.emit('sendDataToEdit', {
          data: dataList
        })
      }
    })
  },

  notifyPageUpdataFoo() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromRoom', {
      data: _this.data.dataList.length
    });
  },


})